import axios from 'axios'
//     axios携带token
//     添加请求拦截器，在请求头中加token
// '''''''''''''''''''''''''''''''''''''''''
const requests = axios.create({
    //配置对象
    //基础路径，发送请求的时候，路径当中会出现api
    baseURL:"/api",
    //配置请求超时时间
    timeout:2000,

});
requests.interceptors.request.use(
    config => {
        if (localStorage.getItem("token")) {
            config.headers.token = localStorage.getItem("token");
        }
        return config;
    },
    error => {
        return Promise.reject(error);
    });
//封装POST请求
export const postRequest=(url,data)=>{
    return axios({
        method:'post',
        url:url,
        data:data
    })
}
//封装GET请求
export const getRequest=(url,params)=>{
    return axios({
        method:'get',
        url:url,
        params
    })
}
//封装put请求
export const putRequest=(url,data)=> {
    return axios({
        method: 'put',
        url: url,
        data: data
    })
}
//封装delete请求
export const deleteRequest=(url,data)=> {
    return axios({
        method: 'delete',
        url: url,
        data: data
    })
}

export default requests;
